﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestOnlineProject.Models.Entities;

namespace TestOnlineProject.Models.DataAccess
{
    public class FeedbackDAO
    {
        private TestOnlineDBEntities db = new TestOnlineDBEntities();

        public List<Feedback> Select()
        {
            try
            {
                var feedbacks = db.Feedbacks.ToList();
                return feedbacks;
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public Feedback SelectByFeedbackId(int id)
        {
            try
            {
                var feedback = db.Feedbacks.FirstOrDefault(f => f.FeedbackId == id);
                return feedback;
            }
            catch (Exception)
            {
                throw;
            }
            
        }
        public int Insert(Feedback feedback)
        {
            var result = 0;
            try
            {
                db.Feedbacks.AddObject(feedback);
                result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                return result;
            }
        }

        public int Update(Feedback feedback)
        {
            try
            {
                var vio = db.Feedbacks.FirstOrDefault(f => f.FeedbackId == feedback.FeedbackId);
                vio.IsChecked = feedback.IsChecked;
                var result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        } 

        public int Delete(int feedbackId)
        {
            try
            {
                var feedback = db.Feedbacks.FirstOrDefault(f => f.FeedbackId == feedbackId);
                db.Feedbacks.DeleteObject(feedback);
                var result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                return 0;
                throw;
            }
        }

        public int DeleteByUserId(int userId)
        {
            try
            {
                var feedbacks = db.Feedbacks.Where(x => x.UserId == userId).ToList();
                foreach (var feedback in feedbacks)
                {
                    db.Feedbacks.DeleteObject(feedback);
                }
                var result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// Select list Feedbacks of User
        /// </summary>
        /// <param name="userId">Id of User</param>
        /// <returns>List Feedbacks</returns>

        public List<Feedback> SelectByUserId(int userId)
        {
            var feedbacks = db.Feedbacks.Where(x => x.UserId == userId).ToList();
            return feedbacks;
        }
    }
}